/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.springframework.beans.factory.InitializingBean;

/**
 * Used to extend existing collection configuration of extendable modules
 * by additional elements provided by other modules via spring.
 * 
 * @author Walter Lütgenau
 */
public abstract class CollectionExtender implements InitializingBean {
    Object container;
    Collection additionalElements;
    
    public void setContainer(Object container) {
        this.container = container;
    }

    public Object getContainer() {
        return container;
    }

    public Collection getAdditionalElements() {
        return additionalElements;
    }
    
    public void setAdditionalElements(Collection additionalElements) {
        this.additionalElements = additionalElements;
    }
    
    protected void insertElements() {
        ArrayList collectionElements = new ArrayList();
        
        Iterator collectionIterator = getCollection().iterator();
        while (collectionIterator.hasNext()) {
            Object collectionElement = collectionIterator.next();
            
            Iterator additionalElementIterator = additionalElements.iterator();
            while (additionalElementIterator.hasNext()) {
                Object additionalElement = additionalElementIterator.next();
                
                if (additionalElement instanceof PositionableAdditionalElement // insertBefore indication
                        && collectionElement.equals(((PositionableAdditionalElement) additionalElement).getInsertBefore())) {
                    collectionElements.add(additionalElement);
                    additionalElementIterator.remove();
                }
            }
            
            collectionElements.add(collectionElement);
            collectionIterator.remove();
        }
        
        getCollection().addAll(collectionElements); // add all elements again, together with the insertBefore indication
        getCollection().addAll(additionalElements); // add remaining additional elements, i.e. without insertBefore indication
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        insertElements();
    }
    
    public abstract Collection getCollection();
    
}
